<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList(1)">
      <el-form-item>
        <el-input v-model="dataForm.keyword.phone" placeholder="输入手机号查找" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <!--<el-button @click="getDataList(1)">查询</el-button>-->
        <el-button @click="readRecord">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
      @row-click="rowClick">
      <el-table-column
        prop="type"
        header-align="center"
        align="center"
        label="宠物类型">
        <template slot-scope="scope">
          <img :src="scope.row.typeUrl" style="width: 60px;height: 60px;border-radius: 50%;">
        </template>
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="宠物ID">
      </el-table-column>
      <el-table-column
        prop="phone"
        header-align="center"
        align="center" min-width="110"
        label="主人手机号">
      </el-table-column>
      <el-table-column
        prop="petLevel"
        header-align="center"
        align="center"
        label="宠物等级">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="宠物名称">
      </el-table-column>
      <el-table-column
        prop="roundLevel"
        header-align="center"
        align="center"
        label="闯关级别">
      </el-table-column>
      <el-table-column
        prop="integral"
        header-align="center"
        align="center"
        label="积分">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="勋章数量"
        column-key="recordHistory">
        <template slot-scope="scope">
          <el-button type="text" size="small">查看勋章</el-button>
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        column-key="recordHistory"
        label="成就称号">
        <template slot-scope="scope">
          <el-button type="text" size="small">查看成就</el-button>
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        column-key="petGoods"
        label="宠物物品">
        <template slot-scope="scope">
          <el-button type="text" size="small">查看物品</el-button>
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        column-key="recordHistory"
        align="center"
        label="阅读报告">
        <template slot-scope="scope">
          <el-button type="text" size="small">查看报告</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <!--弹窗，宠物详情-->
    <detail v-if="detailVisible" ref="detail"></detail>
    <!--弹窗，宠物物品-->
    <goods v-if="goodsVisible" ref="goods"></goods>
    <!--弹窗，阅读记录-->
    <read-record v-if="readRecordVisible" ref="readRecord"></read-record>
  </div>
</template>

<script>
  import AddOrUpdate from './pbpet-add-or-update'
  import Detail from './pbpet-detail'
  import Goods from './pbpet-goods'
  import ReadRecord from './pbpet-read-record'

  export default {
    data () {
      return {
        dataForm: {
          keyword: {
            phone: ''
          }
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        detailVisible: false,
        goodsVisible: false,
        readRecordVisible: false
      }
    },
    components: {
      AddOrUpdate,
      Detail,
      Goods,
      ReadRecord
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList (formSearch) {
        // 搜索框来源
        if (formSearch === 1) {
          this.pageIndex = 1
        }
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/picbook/pbpet/list'),
          method: 'post',
          data: this.$http.adornData({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keyword': this.dataForm.keyword
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      //详情
      detail (id) {
        this.detailVisible = true
        this.$nextTick(() => {
          this.$refs.detail.init(id)
        })
      },
      // 物品
      goods (id) {
        this.goodsVisible = true
        this.$nextTick(() => {
          this.$refs.goods.init(id)
        })
      },
      //阅读记录
      readRecord (id) {
        this.readRecordVisible = true
        this.$nextTick(() => {
          this.$refs.readRecord.init(id)
        })
      },
      rowClick (row, event, column) {
        if (column.columnKey == 'recordHistory') {
          this.readRecord(row.id)
        } else if (column.columnKey == 'petGoods') {
          this.goods(row.id)
        } else {
          this.detail(row.id)
        }
      }
    }
  }
</script>
